#include<stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <windows.h>

#define high 20//画面尺寸 
#define width 50
//全局变量

int canvas[high][width]={0};//对应元素  0为空格  1为砖块￥  2为小球o  3为挡板*  
int ball_x,ball_y;//小球坐标 
int vx,vy;//小球速度
int r;// 挡板半径
int left,right;//挡板左右边界 
int posx,posy;//挡板中心坐标
int score;
 
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x,int y)
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}

void startup()
{
	HideCursor(); 
	
	int i,j;
	
	score=0;
	
	posx=high-1;
	posy=width/2;//挡板 
	r=4;
	left=posy-r;
	right=posy+r;
	for(i=left;i<=right;i++)
	{
		canvas[high-2][i]=3;//定义挡板初始值
	}
	 
	
	ball_x=high/2;
	ball_y=width/2;//小球 
	vx=1;
	vy=1;
	canvas[ball_x][ball_y]=2;//定义小球初始值 
	
	for(i=1;i<width-1;i++)//定义砖块初始值 
	{
		canvas[1][i]=1;
	}
	for(i=1;i<width-1;i++) 
	{
		canvas[2][i]=1;
	}
	for(i=1;i<width-1;i++) 
	{
		canvas[3][i]=1;
	}
}

void show()
{
	gotoxy(0,0);
	
		int i,j;
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if(canvas[i][j]==3) 
			{
				printf("*");//输出挡板 
			}
			else if(canvas[i][j]==2) 
			{
				printf("o");//输出小球 
			}
			else if(canvas[i][j]==1) //输出砖块 
			{
				printf("$");
			}
			else if(i==0&&j<width)//输出边框 
			{
				printf("#");
			}
			else if(i==high-1&&j<width) 
			{
				printf("#");
			}
			else if(j==0&&i<high) 
			{
				printf("#");
			}
			else if(j==width-1&&i<high) 
			{
				printf("#");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	 } 
	printf("得分：%d\n",score);
}

void updatewithinput()
{
	int i; 
	char input;
	if(kbhit())
	{
		input=getch();
		
		if(input=='a')                              //挡板移动 
		{
			for(i=left;i<=right;i++)
			{
				canvas[high-2][i]=0;//挡板初始值变为0 
			}
			posy--;
			left=posy-r;
			right=posy+r;
			for(i=left;i<=right;i++)
			{
				canvas[high-2][i]=3;//定义挡板初始值
			}
		}
		if(input=='d')
		{
			for(i=left;i<=right;i++)
			{
				canvas[high-2][i]=0;//挡板初始值变为0 
			}
			posy++;
			left=posy-r;
			right=posy+r;
			for(i=left;i<=right;i++)
			{
			canvas[high-2][i]=3;//定义挡板初始值
			}
		}
		
		if(input==27)
		{
			system("pause");
		}	
	}	
}

void updatewithoutinput()
{
	int i,j;
	static int speed=0;
	//防止挡板超出边缘 
	if(left==0)
	{
		for(i=left;i<=right;i++)
			{
				canvas[high-2][i]=0;//挡板初始值变为0 
			}
			posy++;
			left=posy-r;
			right=posy+r;
			for(i=left;i<=right;i++)
			{
			canvas[high-2][i]=3;//定义挡板初始值
			}
	}
	if(right==width-1)
	{
		for(i=left;i<=right;i++)
			{
				canvas[high-2][i]=0;//挡板初始值变为0 
			}
			posy--;
			left=posy-r;
			right=posy+r;
			for(i=left;i<=right;i++)
			{
			canvas[high-2][i]=3;//定义挡板初始值
			}
	}
	//死亡控制
		if((ball_x==high-3)&&((ball_y>right)||(ball_y<left)))
	{
		printf("game over!!!!");
		exit(0);
	} 
	//控制小球速度 
	 if(speed<10)
	 {
	 	speed++;
	 }
	 if(speed==10)
	 {
		
	 	//小球反弹
		if(ball_x==high-3) 
		 {
			vx=-1*vx;
		 		
		 }
		 //小球反弹 
	 	if(ball_y==1||ball_y==width-2) 
		 {
		 	vy=-1*vy;
		 }
		//消砖块控制 
		if(ball_x>=1&&ball_x<=3)
		{
			if(canvas[ball_x-1][ball_y]==1)
		 	{
		 		vx=-1*vx;
	 			score++;
			}
		}
		 
	 	//小球移动 
	 	canvas[ball_x][ball_y]=0;//小球初始值变为0 
	 	ball_x=ball_x+vx;
	 	ball_y=ball_y+vy;
	 	canvas[ball_x][ball_y]=2;//定义小球初始值
	
	 	speed=0;
	 }
	

		
}

int main()
{
	startup();
	
	while(1)
	{
		show();
		updatewithinput();
		updatewithoutinput();
	}	
	return 0;
 } 